package photobox.manage

import grails.converters.JSON
import photobox.BaseController
import photobox.account.ApplyForPhotographer
import photobox.account.User
import photobox.utils.ParamUtils
import photobox.utils.ResultUtil

class MngApplyForPhotographerController extends BaseController {
 def applyForPhotographerService
    def index() {}
    def list() {
        if(params.agreementId){
            def applyForPhotographer = ApplyForPhotographer.get(params.agreementId)
            if(params.setStatus =="1"){            //同意
                applyForPhotographer.agreement =applyForPhotographer.AGREEMENT_YES
                applyForPhotographer.save()
                def user = User.get(applyForPhotographer.userId)
                user.isPhotographer = User.IS_PHOTOGRAPHER_YES
                user.save()
                applyForPhotographerService.saveNotice(applyForPhotographer,"恭喜你！审核通过")
            }else  if(params.setStatus == "2"){  //不同意
                applyForPhotographer.agreement =applyForPhotographer.AGREEMENT_NO
                applyForPhotographer.save()
                applyForPhotographerService.saveNotice(applyForPhotographer,"很遗憾！审核没有通过")
            }
        }
        def searchParams = ParamUtils.parseFilter(params.filters)
        def pageParams = ParamUtils.parsePage(params)
        def sortParams = ParamUtils.parseSort(params,
                ["id", "user", "content", "dateCreated","lastUpdated","agreement"], [sort: 'id', order: "desc"])

        def applicationRs = applyForPhotographerService.list(searchParams, pageParams, sortParams)
        println(applicationRs.rs)

        def nameList = []
        applicationRs.rs.each{ r ->
            nameList.push(
                 id:r.id,
                 userName:r.user.username,
                 content:r.content,
                 dateCreated:r.dateCreated,
                 lastUpdated:r.lastUpdated,
                 agreement:r.agreement
            )
        }

        render ResultUtil.pageResults(nameList, applicationRs.total, pageParams,
                [id: 'id', userName: "userName", content: 'content', dateCreated: 'dateCreated', lastUpdated: 'lastUpdated',agreement:'agreement']) as JSON
    }
}
